package threadTestDemo.bank;

import java.math.BigDecimal;

public class BankRun implements Runnable {

    BigDecimal oldBalanceFrom= BigDecimal.valueOf(0);
    BigDecimal newBalanceFrom= BigDecimal.valueOf(0);

    BigDecimal oldBalanceTo= BigDecimal.valueOf(0);
    BigDecimal newBalanceTo= BigDecimal.valueOf(0);


    String accountFrom;
    String accountTo;
    BigDecimal changeNumber;
    OperateType operateType;

    public BankRun(TransferMoneyDto dto){
        this.accountFrom=dto.accountFrom;
        this.accountTo=dto.accountTo;
        this.changeNumber=dto.changeNumber;
        this.operateType= dto.operateType;
    }

    //查询余额
    public BigDecimal selectBalance(String account){
        BigDecimal balance= BigDecimal.valueOf(100);
        return balance;
    }

    @Override
    public void run() {

    oldBalanceFrom=selectBalance(accountFrom);
    oldBalanceTo=selectBalance(accountTo);

        if (operateType == OperateType.add) {
            newBalanceFrom=oldBalanceFrom.add(changeNumber);
            newBalanceTo=oldBalanceTo.subtract(changeNumber);
        }else if(operateType==OperateType.reduce) {
            newBalanceFrom=oldBalanceFrom.subtract(changeNumber);
            newBalanceTo=oldBalanceTo.add(changeNumber);
        }
    }
}
